﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace BusinessLogic
{
    public static class ChiTietTraLaiNCC
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.ChiTietTraLaiNCC[] Select()
        {
            Entities.ChiTietTraLaiNCC[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietTraLaiNCC.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietTraLaiNCC ChiTietTraLaiNCC = new Entities.ChiTietTraLaiNCC();
                    ChiTietTraLaiNCC.MaHDTraLaiNCC = dr[Common.Constants.ChiTietTraLaiNCC.MaHDTraLaiNCC].ToString();
                    ChiTietTraLaiNCC.MaHangHoa = dr[Common.Constants.ChiTietTraLaiNCC.MaHangHoa].ToString();
                    ChiTietTraLaiNCC.TenHangHoa = dr[Common.Constants.ChiTietTraLaiNCC.TenHangHoa].ToString();
                    ChiTietTraLaiNCC.SoLuong = int.Parse(dr[Common.Constants.ChiTietTraLaiNCC.SoLuong].ToString());
                    ChiTietTraLaiNCC.MaHoaDon = (dr[Common.Constants.ChiTietTraLaiNCC.MaHoaDon].ToString());
                    ChiTietTraLaiNCC.DonGia = (dr[Common.Constants.ChiTietTraLaiNCC.DonGia].ToString());
                    ChiTietTraLaiNCC.ThanhTien = (dr[Common.Constants.ChiTietTraLaiNCC.ThanhTien].ToString());
                    ChiTietTraLaiNCC.Thue = (dr[Common.Constants.ChiTietTraLaiNCC.Thue].ToString());
                    ChiTietTraLaiNCC.PhanTramChietKhau = dr[Common.Constants.ChiTietTraLaiNCC.PhanTramChietKhau].ToString();
                    ChiTietTraLaiNCC.GhiChu = dr[Common.Constants.ChiTietTraLaiNCC.GhiChu].ToString();
                    ChiTietTraLaiNCC.Deleted = bool.Parse(dr[Common.Constants.ChiTietTraLaiNCC.Deleted].ToString());
                    arr.Add(ChiTietTraLaiNCC);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietTraLaiNCC[0];
                }
                else
                {
                    list = new Entities.ChiTietTraLaiNCC[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietTraLaiNCC)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietTraLaiNCC[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.ChiTietTraLaiNCC ChiTietTraLaiNCC)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietTraLaiNCC.InsertUpdate;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.HanhDong, SqlDbType.NVarChar, 50).Value = ChiTietTraLaiNCC.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.MaHDTraLaiNCC, SqlDbType.VarChar, 50).Value = ChiTietTraLaiNCC.MaHDTraLaiNCC;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.MaHangHoa, SqlDbType.VarChar, 50).Value = ChiTietTraLaiNCC.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.TenHangHoa, SqlDbType.NVarChar, 200).Value = ChiTietTraLaiNCC.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.SoLuong, SqlDbType.Int).Value = ChiTietTraLaiNCC.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.DonGia, SqlDbType.Float).Value = ChiTietTraLaiNCC.DonGia;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.PhanTramChietKhau, SqlDbType.Float).Value = ChiTietTraLaiNCC.PhanTramChietKhau;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.Thue, SqlDbType.Float).Value = ChiTietTraLaiNCC.Thue;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.ThanhTien, SqlDbType.Float).Value = ChiTietTraLaiNCC.ThanhTien;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.MaHoaDon, SqlDbType.VarChar, 50).Value = ChiTietTraLaiNCC.MaHoaDon;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.GhiChu, SqlDbType.NVarChar, 200).Value = ChiTietTraLaiNCC.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.Deleted, SqlDbType.Bit).Value = ChiTietTraLaiNCC.Deleted;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }

        public static int InsertUpdate(Entities.ChiTietTraLaiNCC[] ChiTietTraLaiNCC)
        {
            try
            {
                foreach (Entities.ChiTietTraLaiNCC item in ChiTietTraLaiNCC)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region insert update
        public static int Delete(Entities.ChiTietTraLaiNCC ChiTietTraLaiNCC)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietTraLaiNCC.Delete;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietTraLaiNCC.MaHDTraLaiNCC, SqlDbType.VarChar, 50).Value = ChiTietTraLaiNCC.MaHDTraLaiNCC;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
        #endregion
    }
}
#endregion